package login.android;



import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.content.SharedPreferences;

public class LoginLayout extends Activity {
    EditText un,pw;
	TextView error;
    Button ok;
    /** Called when the activity is first created. */
    
    String username, paswd;
    int duration = Toast.LENGTH_SHORT;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        un=(EditText)findViewById(R.id.et_un);
        pw=(EditText)findViewById(R.id.et_pw);
        ok=(Button)findViewById(R.id.btn_login);
        error=(TextView)findViewById(R.id.tv_error);
        cargarConfiguracion();
        
        ok.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
            	
            	loginApp(un.getText().toString(),pw.getText().toString());
            	
            	
            }
        });
  
        
    }
  
   
   
  //en el evento "Cerrar aplicacion" guardar los datos en fichero xml
    @Override
    public void onDestroy()
    {	Context context = getApplicationContext();
    	Toast toast3 = Toast.makeText(context, username, duration);
		toast3.show();
    	guardarConfiguracion( username,  paswd);
    	super.onDestroy();
      
    }
    
    //en el evento "Abrir aplicacion" leemos los datos de configuración del fichero xml
    @Override
    protected void onStart() 
    {
    	cargarConfiguracion();
    	super.onStart();
    	
    }    
  //guardar configuracion aplicacion Android usando SharedPreferences
    public void guardarConfiguracion(String user, String pass) {
    	SharedPreferences prefs = getSharedPreferences("ficheroconfiguracion", Context.MODE_PRIVATE);
    	SharedPreferences.Editor editor = prefs.edit();
        editor.putString("username", user);
        editor.putString("password", pass);
        editor.commit();
      }
  //cargar configuracion aplicacion Android usando SharedPreferences
    public void cargarConfiguracion()
    {    	
    	SharedPreferences prefs = getSharedPreferences("ficheroconfiguracion", Context.MODE_PRIVATE);
    	username = prefs.getString("username", un.getText().toString());
        paswd = prefs.getString("password", pw.getText().toString());
        Context context = getApplicationContext();
    	Toast toast3 = Toast.makeText(context, username, duration);
		toast3.show();
        if(!(username.equals("") || paswd.equals(""))){
        	loginApp(username,paswd);
        }
    }
    
    public void loginApp(String user, String pass){
    	
    	ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    	postParameters.add(new BasicNameValuePair("username", user));
    	postParameters.add(new BasicNameValuePair("password", pass));

    	String response = null;
    	try {
    	    response = CustomHttpClient.executeHttpPost("http://proyecto.cambiarbolivarescomprareuros.com/wordpress/android/login.php", postParameters);
    	    String res=response.toString();
    	    res= res.replaceAll("\\s+","");
    	    if(res.equals("1")){
    	    	error.setText("Correct Username or Password");
    	    	guardarConfiguracion(user,pass);	
    	    	Intent intent =  new Intent(LoginLayout.this, Menu.class);
    	    	startActivity(intent);
    	    }else
    	    	error.setText("Sorry!! Incorrect Username or Password");
    	} catch (Exception e) {
    		un.setText(e.toString());
    	}
    	
    }
}
